#define  _CRT_SECURE_NO_WARNINGS
class Solution {
public:
    int triangleNumber(vector<int>& nums) {
        int ret = 0;
        sort(nums.begin(), nums.end());
        for (int i_max = nums.size() - 1; i_max >= 2; i_max--)
        {
            int right = i_max - 1, left = 0;
            while (left < right)
            {
                if (nums[left] + nums[right] <= nums[i_max])
                {
                    left++;
                }
                else
                {
                    ret += right - left;
                    right--;
                }
            }
        }
        return ret;
    }
};